package dd.prog2.exercises.set04;

public class DefaultVector implements Vector {
	private double[] v;
	private int vLength;

	protected DefaultVector(int vDim) {
		this.v = new double[vDim];
		this.vLength = vDim;
	}

	@Override
	public Vector add(Vector v2) throws ArithmeticException {
		if(v2.getLength() != this.getLength())
			throw new ArithmeticException();
		Vector res = new DefaultVector(this.getLength());
		for (int i = 0; i < res.getLength(); i++) {
			res.setVi(this.getVi(i) + v2.getVi(i), i);
		}
		res.toString();
		return res;
	}

	@Override
	public Vector sub(Vector v2) throws ArithmeticException {
		if(v2.getLength() != this.getLength())
			throw new ArithmeticException();
		Vector res = new DefaultVector(this.getLength());
		for (int i = 0; i < res.getLength(); i++) {
			res.setVi(this.getVi(i) - v2.getVi(i), i);
		}
		return res;
	}

	@Override
	public double mul(Vector v2) throws ArithmeticException {
		if(v2.getLength() != this.getLength())
			throw new ArithmeticException();
		double res = 0;
		for (int i = 0; i < this.vLength; i++) {
			res += this.getVi(i) * v2.getVi(i);
		}
		return res;
	}

	@Override
	public String toString() {
		String result = "V = [ ";
		for (int i = 0; i < v.length; i++) {
			result += v[i] + " ";
		}
		return result + "]\n";
	}

	@Override
	public void setVi(double value, int index) {
		v[index] = value;

	}

	@Override
	public void setall(double value) {
		for (int i = 0; i < v.length; i++) {
			v[i] = value;
		}

	}

//- - - - - - - - - getter
	@Override
	public double getVi(int index) {
		return v[index];
	}

	@Override
	public int getLength() {
		return this.vLength;
	}
}
